package com.foreknow.dao.impl;

import com.foreknow.dao.AdimDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.model.Admin;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDAO implements AdimDao {

    @Override
    public boolean save(Admin admin) throws SQLException {
        String sql = "insert into admin(username,sex,password)";
        int i = jdbcTemplate.update(sql,admin.getName()+admin.getSex()+admin.getPassword());
        return i==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql ="select * from admin";
        EntityMapping mapping = mappingFactory.getMap("adminapping");
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public Admin getQueryById(int id) throws SQLException {
        String sql = "select * from admin where id=?";
        EntityMapping mapping = mappingFactory.getMap("adminapping");
        List<Object> a = jdbcTemplate.query(sql,mapping,id);
        if (a.size()>0){
            return ((Admin) a.get(0));
        }else {
            return null;
        }
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql = "delete from admin where id=?";
        int i = jdbcTemplate.update(sql,id);
        return i==1;
    }
}
